package com.rsk.service.impl;


import java.util.*;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.SysMenuRepository;
import com.rsk.dao.extend.SysMenuExtend;
import com.rsk.dao.utils.SearchDataUtils;
import com.rsk.entity.SysMenu;
import com.rsk.service.SysMenuService;
import com.rsk.utils.algorithm.RecursionAlgorithm;
import com.rsk.utils.algorithm.SnowflakeIdWorker;


@Service(interfaceClass = SysMenuService.class)
public class SysMenuServiceImpl implements SysMenuService{
	
	@Autowired
	private SysMenuRepository sysMenuRepository;
	
	@Autowired
	private SysMenuExtend sysMenuExtend;
	
	@Autowired
	private SearchDataUtils searchDataUtils;

	//保存菜单信息
	@Transactional
	public int saveSysMenu(String menuName, String menuUrl, Long pId,
			Integer sort, Long operatorUser) {
		SysMenu sysMenu = new SysMenu();
		sysMenu.setId(SnowflakeIdWorker.generateId(true));
		sysMenu.setMenuName(menuName);
		sysMenu.setMenuUrl(menuUrl);
		sysMenu.setPId(pId);
		sysMenu.setSort(sort);
		sysMenu.setOperatorUser(operatorUser);
		sysMenu.setOperatorTime(new Date());
		sysMenuRepository.saveAndFlush(sysMenu);
		return 1;
	}

	//修改菜单信息
	@Transactional
	public int updateSysMenu(Long id, String menuName, String menuUrl,
			Integer sort, Long operatorUser) {
		return sysMenuRepository.updateSysMenu(id, menuName, menuUrl, sort, operatorUser);
	}

	//删除菜单信息
	@Transactional
	public int delSysMenu(Long id, Long operatorUser) {
		return sysMenuRepository.delSysMenu(id, operatorUser);
	}

	//查询菜单信息
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Map> selectMenu(String menuName, String menuUrl) {
		 Map<String, Object> map = sysMenuExtend.selectMenus(menuName, menuUrl);
	     String sql = map.get("sql").toString();
	     Map<String,Object> paramMap = (Map<String, Object>) map.get("paramMap");
	     List<Map> list = searchDataUtils.search(sql, paramMap, Map.class);
	     list = RecursionAlgorithm.menuList(list, "id", "p_id");
	     return list;
	}
	
}
